package com.aixingfu.sports.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aixingfu.sports.R;
import com.aixingfu.sports.bean.PaymentBean;
import com.orie.librarybase.utils.MoneyEditUtils;

import java.util.List;

/**
 * Created by 吕冬冬 on 2019.04.09.
 *
 * @auhtor 吕冬冬
 * email 863964634@qq.com
 * 内容:
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
public class PaymentTypeAdapter extends RecyclerView.Adapter<PaymentTypeAdapter.MainViewHolder> {

        LayoutInflater inflater;
        List<PaymentBean> modelList;
        private int mposition;
    private OnItemEditTextChangedListener mListener;

    public void setListener(OnItemEditTextChangedListener listener) {
        mListener = listener;
    }
    public void setNewData( List<PaymentBean>  modelist) {
        this.modelList = modelist;
    }
        public PaymentTypeAdapter(Context context, List<PaymentBean> list) {
            inflater = LayoutInflater.from(context);
            modelList = list;
        }
    public List<PaymentBean> getData() {
        return modelList;
    }
        public void setList(List<PaymentBean> list) {
            this.modelList = list;
            notifyDataSetChanged();
        }
    public PaymentBean getItem(int position) {
        if (position < modelList.size()) {
            return modelList.get(position);
        } else {
            return null;
        }
    }
        @Override
        public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = inflater.inflate(R.layout.item_payment_type, parent, false);
            return new MainViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MainViewHolder holder, int position) {
            holder.setIsRecyclable(false);
            holder.bindData(modelList.get(position),position);
        }
    public interface OnItemEditTextChangedListener {
        void onEditTextAfterTextChanged(Editable editable, int position);
    }
        @Override
        public int getItemCount() {
            return modelList.size();
        }

        class MainViewHolder extends RecyclerView.ViewHolder {

            TextView payment_method;
            EditText money;
            LinearLayout ll_pay_nethod;

            public MainViewHolder(View itemView) {
                super(itemView);
                payment_method = (TextView) itemView.findViewById(R.id.tv_payment_method);
                money = (EditText) itemView.findViewById(R.id.ed_money);
                ll_pay_nethod = (LinearLayout) itemView.findViewById(R.id.ll_payment_method);
                MoneyEditUtils.afterDotTwo(money);
            }

            public void bindData(PaymentBean rowModel,int position) {
                if ("请选择付款方式".equals(rowModel.getPayment())){
                    payment_method.setHint("请选择付款方式");
                }else {
                    payment_method.setText(rowModel.getPayment());
                }
                money.setText(rowModel.getPrice());
                ll_pay_nethod.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mItemClickListener.onItemClick(position,v);
                    }
                });
                money.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }
                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        mListener.onEditTextAfterTextChanged(s, position);
                    }
                });
            }
        }
    protected OnItemClickListener mItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mItemClickListener = listener;
    }
}
